<template>
  <div class="equipment-management">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h2>设备管理</h2>
        <p>设备档案、维护计划、故障管理</p>
      </div>
      <div class="header-actions">
        <el-button type="primary" @click="handleAdd">
          <el-icon><Plus /></el-icon>
          新增设备
        </el-button>
        <el-button @click="handleImport">
          <el-icon><Upload /></el-icon>
          导入设备
        </el-button>
        <el-button @click="handleExport">
          <el-icon><Download /></el-icon>
          导出数据
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon running">
                <el-icon><Setting /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ stats.running }}</div>
                <div class="stat-label">运行中设备</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon maintenance">
                <el-icon><Tools /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ stats.maintenance }}</div>
                <div class="stat-label">维护中设备</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon fault">
                <el-icon><Warning /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ stats.fault }}</div>
                <div class="stat-label">故障设备</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon efficiency">
                <el-icon><TrendCharts /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ stats.efficiency }}%</div>
                <div class="stat-label">设备效率</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 图表区域 -->
    <div class="charts-section">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-card title="设备状态分布">
            <div ref="statusChartRef" style="height: 300px;"></div>
          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card title="设备效率趋势">
            <div ref="efficiencyChartRef" style="height: 300px;"></div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 主要内容区域 -->
    <el-card class="main-content">
      <!-- 标签页 -->
      <el-tabs v-model="activeTab" @tab-change="handleTabChange">
        <!-- 设备档案 -->
        <el-tab-pane label="设备档案" name="equipment">
          <!-- 筛选区域 -->
          <div class="filter-section">
            <el-form :model="equipmentFilters" inline>
              <el-form-item label="设备编号">
                <el-input v-model="equipmentFilters.code" placeholder="请输入设备编号" clearable />
              </el-form-item>
              <el-form-item label="设备名称">
                <el-input v-model="equipmentFilters.name" placeholder="请输入设备名称" clearable />
              </el-form-item>
              <el-form-item label="设备类型">
                <el-select v-model="equipmentFilters.type" placeholder="请选择设备类型" clearable>
                  <el-option label="生产设备" value="production" />
                  <el-option label="检测设备" value="testing" />
                  <el-option label="辅助设备" value="auxiliary" />
                </el-select>
              </el-form-item>
              <el-form-item label="设备状态">
                <el-select v-model="equipmentFilters.status" placeholder="请选择设备状态" clearable>
                  <el-option label="运行中" value="running" />
                  <el-option label="维护中" value="maintenance" />
                  <el-option label="故障" value="fault" />
                  <el-option label="停机" value="stopped" />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="searchEquipment">
                  <el-icon><Search /></el-icon>
                  搜索
                </el-button>
                <el-button @click="resetEquipmentFilters">重置</el-button>
              </el-form-item>
            </el-form>
          </div>

          <!-- 设备列表 -->
          <el-table :data="equipmentList" stripe border>
            <el-table-column prop="code" label="设备编号" width="120" />
            <el-table-column prop="name" label="设备名称" width="150" />
            <el-table-column prop="type" label="设备类型" width="100">
              <template #default="{ row }">
                <el-tag :type="getEquipmentTypeTag(row.type)">{{ getEquipmentTypeText(row.type) }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="model" label="型号规格" width="120" />
            <el-table-column prop="manufacturer" label="制造商" width="120" />
            <el-table-column prop="location" label="安装位置" width="120" />
            <el-table-column prop="status" label="设备状态" width="100">
              <template #default="{ row }">
                <el-tag :type="getStatusTag(row.status)">{{ getStatusText(row.status) }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="efficiency" label="设备效率" width="100">
              <template #default="{ row }">
                <span>{{ row.efficiency }}%</span>
              </template>
            </el-table-column>
            <el-table-column prop="purchaseDate" label="采购日期" width="120" />
            <el-table-column label="操作" width="200" fixed="right">
              <template #default="{ row }">
                <el-button size="small" @click="handleView(row)">查看</el-button>
                <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
                <el-button size="small" type="warning" @click="handleMaintenance(row)">维护</el-button>
                <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页 -->
          <div class="pagination-wrapper">
            <el-pagination
              v-model:current-page="equipmentPagination.current"
              v-model:page-size="equipmentPagination.size"
              :total="equipmentPagination.total"
              :page-sizes="[10, 20, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleEquipmentSizeChange"
              @current-change="handleEquipmentCurrentChange"
            />
          </div>
        </el-tab-pane>

        <!-- 维护计划 -->
        <el-tab-pane label="维护计划" name="maintenance">
          <!-- 筛选区域 -->
          <div class="filter-section">
            <el-form :model="maintenanceFilters" inline>
              <el-form-item label="设备编号">
                <el-input v-model="maintenanceFilters.equipmentCode" placeholder="请输入设备编号" clearable />
              </el-form-item>
              <el-form-item label="维护类型">
                <el-select v-model="maintenanceFilters.type" placeholder="请选择维护类型" clearable>
                  <el-option label="预防性维护" value="preventive" />
                  <el-option label="纠正性维护" value="corrective" />
                  <el-option label="改进性维护" value="improvement" />
                </el-select>
              </el-form-item>
              <el-form-item label="维护状态">
                <el-select v-model="maintenanceFilters.status" placeholder="请选择维护状态" clearable>
                  <el-option label="计划中" value="planned" />
                  <el-option label="进行中" value="ongoing" />
                  <el-option label="已完成" value="completed" />
                  <el-option label="已取消" value="cancelled" />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="searchMaintenance">
                  <el-icon><Search /></el-icon>
                  搜索
                </el-button>
                <el-button @click="resetMaintenanceFilters">重置</el-button>
                <el-button type="success" @click="handleAddMaintenance">
                  <el-icon><Plus /></el-icon>
                  新增计划
                </el-button>
              </el-form-item>
            </el-form>
          </div>

          <!-- 维护计划列表 -->
          <el-table :data="maintenanceList" stripe border>
            <el-table-column prop="planCode" label="计划编号" width="120" />
            <el-table-column prop="equipmentCode" label="设备编号" width="120" />
            <el-table-column prop="equipmentName" label="设备名称" width="150" />
            <el-table-column prop="type" label="维护类型" width="120">
              <template #default="{ row }">
                <el-tag :type="getMaintenanceTypeTag(row.type)">{{ getMaintenanceTypeText(row.type) }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="plannedDate" label="计划日期" width="120" />
            <el-table-column prop="actualDate" label="实际日期" width="120" />
            <el-table-column prop="responsible" label="负责人" width="100" />
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="getMaintenanceStatusTag(row.status)">{{ getMaintenanceStatusText(row.status) }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="cost" label="维护成本" width="100">
              <template #default="{ row }">
                <span>¥{{ row.cost }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="180" fixed="right">
              <template #default="{ row }">
                <el-button size="small" @click="handleViewMaintenance(row)">查看</el-button>
                <el-button size="small" type="primary" @click="handleEditMaintenance(row)">编辑</el-button>
                <el-button size="small" type="success" @click="handleExecuteMaintenance(row)">执行</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页 -->
          <div class="pagination-wrapper">
            <el-pagination
              v-model:current-page="maintenancePagination.current"
              v-model:page-size="maintenancePagination.size"
              :total="maintenancePagination.total"
              :page-sizes="[10, 20, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleMaintenanceSizeChange"
              @current-change="handleMaintenanceCurrentChange"
            />
          </div>
        </el-tab-pane>

        <!-- 故障管理 -->
        <el-tab-pane label="故障管理" name="fault">
          <!-- 筛选区域 -->
          <div class="filter-section">
            <el-form :model="faultFilters" inline>
              <el-form-item label="设备编号">
                <el-input v-model="faultFilters.equipmentCode" placeholder="请输入设备编号" clearable />
              </el-form-item>
              <el-form-item label="故障等级">
                <el-select v-model="faultFilters.level" placeholder="请选择故障等级" clearable>
                  <el-option label="轻微" value="minor" />
                  <el-option label="一般" value="normal" />
                  <el-option label="严重" value="serious" />
                  <el-option label="紧急" value="urgent" />
                </el-select>
              </el-form-item>
              <el-form-item label="处理状态">
                <el-select v-model="faultFilters.status" placeholder="请选择处理状态" clearable>
                  <el-option label="待处理" value="pending" />
                  <el-option label="处理中" value="processing" />
                  <el-option label="已解决" value="resolved" />
                  <el-option label="已关闭" value="closed" />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="searchFault">
                  <el-icon><Search /></el-icon>
                  搜索
                </el-button>
                <el-button @click="resetFaultFilters">重置</el-button>
                <el-button type="danger" @click="handleAddFault">
                  <el-icon><Plus /></el-icon>
                  故障报告
                </el-button>
              </el-form-item>
            </el-form>
          </div>

          <!-- 故障列表 -->
          <el-table :data="faultList" stripe border>
            <el-table-column prop="faultCode" label="故障编号" width="120" />
            <el-table-column prop="equipmentCode" label="设备编号" width="120" />
            <el-table-column prop="equipmentName" label="设备名称" width="150" />
            <el-table-column prop="faultType" label="故障类型" width="120" />
            <el-table-column prop="level" label="故障等级" width="100">
              <template #default="{ row }">
                <el-tag :type="getFaultLevelTag(row.level)">{{ getFaultLevelText(row.level) }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="reportTime" label="报告时间" width="150" />
            <el-table-column prop="reporter" label="报告人" width="100" />
            <el-table-column prop="status" label="处理状态" width="100">
              <template #default="{ row }">
                <el-tag :type="getFaultStatusTag(row.status)">{{ getFaultStatusText(row.status) }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="handler" label="处理人" width="100" />
            <el-table-column label="操作" width="180" fixed="right">
              <template #default="{ row }">
                <el-button size="small" @click="handleViewFault(row)">查看</el-button>
                <el-button size="small" type="primary" @click="handleProcessFault(row)">处理</el-button>
                <el-button size="small" type="success" @click="handleResolveFault(row)">解决</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页 -->
          <div class="pagination-wrapper">
            <el-pagination
              v-model:current-page="faultPagination.current"
              v-model:page-size="faultPagination.size"
              :total="faultPagination.total"
              :page-sizes="[10, 20, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleFaultSizeChange"
              @current-change="handleFaultCurrentChange"
            />
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 设备详情对话框 -->
    <el-dialog v-model="equipmentDialogVisible" :title="equipmentDialogTitle" width="800px">
      <el-form :model="equipmentForm" :rules="equipmentRules" ref="equipmentFormRef" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设备编号" prop="code">
              <el-input v-model="equipmentForm.code" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="equipmentForm.name" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设备类型" prop="type">
              <el-select v-model="equipmentForm.type" placeholder="请选择设备类型">
                <el-option label="生产设备" value="production" />
                <el-option label="检测设备" value="testing" />
                <el-option label="辅助设备" value="auxiliary" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号规格" prop="model">
              <el-input v-model="equipmentForm.model" placeholder="请输入型号规格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="制造商" prop="manufacturer">
              <el-input v-model="equipmentForm.manufacturer" placeholder="请输入制造商" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装位置" prop="location">
              <el-input v-model="equipmentForm.location" placeholder="请输入安装位置" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="采购日期" prop="purchaseDate">
              <el-date-picker v-model="equipmentForm.purchaseDate" type="date" placeholder="请选择采购日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购价格" prop="purchasePrice">
              <el-input-number v-model="equipmentForm.purchasePrice" :min="0" placeholder="请输入采购价格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="设备描述" prop="description">
          <el-input v-model="equipmentForm.description" type="textarea" :rows="3" placeholder="请输入设备描述" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="equipmentDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleEquipmentSubmit">确定</el-button>
      </template>
    </el-dialog>

    <!-- 维护计划对话框 -->
    <el-dialog v-model="maintenanceDialogVisible" :title="maintenanceDialogTitle" width="600px">
      <el-form :model="maintenanceForm" :rules="maintenanceRules" ref="maintenanceFormRef" label-width="100px">
        <el-form-item label="设备" prop="equipmentId">
          <el-select v-model="maintenanceForm.equipmentId" placeholder="请选择设备" filterable>
            <el-option
              v-for="equipment in equipmentOptions"
              :key="equipment.id"
              :label="`${equipment.code} - ${equipment.name}`"
              :value="equipment.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="维护类型" prop="type">
          <el-select v-model="maintenanceForm.type" placeholder="请选择维护类型">
            <el-option label="预防性维护" value="preventive" />
            <el-option label="纠正性维护" value="corrective" />
            <el-option label="改进性维护" value="improvement" />
          </el-select>
        </el-form-item>
        <el-form-item label="计划日期" prop="plannedDate">
          <el-date-picker v-model="maintenanceForm.plannedDate" type="date" placeholder="请选择计划日期" />
        </el-form-item>
        <el-form-item label="负责人" prop="responsible">
          <el-input v-model="maintenanceForm.responsible" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="维护内容" prop="content">
          <el-input v-model="maintenanceForm.content" type="textarea" :rows="3" placeholder="请输入维护内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="maintenanceDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleMaintenanceSubmit">确定</el-button>
      </template>
    </el-dialog>

    <!-- 故障报告对话框 -->
    <el-dialog v-model="faultDialogVisible" :title="faultDialogTitle" width="600px">
      <el-form :model="faultForm" :rules="faultRules" ref="faultFormRef" label-width="100px">
        <el-form-item label="设备" prop="equipmentId">
          <el-select v-model="faultForm.equipmentId" placeholder="请选择设备" filterable>
            <el-option
              v-for="equipment in equipmentOptions"
              :key="equipment.id"
              :label="`${equipment.code} - ${equipment.name}`"
              :value="equipment.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="故障类型" prop="faultType">
          <el-input v-model="faultForm.faultType" placeholder="请输入故障类型" />
        </el-form-item>
        <el-form-item label="故障等级" prop="level">
          <el-select v-model="faultForm.level" placeholder="请选择故障等级">
            <el-option label="轻微" value="minor" />
            <el-option label="一般" value="normal" />
            <el-option label="严重" value="serious" />
            <el-option label="紧急" value="urgent" />
          </el-select>
        </el-form-item>
        <el-form-item label="故障描述" prop="description">
          <el-input v-model="faultForm.description" type="textarea" :rows="4" placeholder="请详细描述故障现象" />
        </el-form-item>
        <el-form-item label="报告人" prop="reporter">
          <el-input v-model="faultForm.reporter" placeholder="请输入报告人" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="faultDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleFaultSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, nextTick } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Plus,
  Upload,
  Download,
  Search,
  Setting,
  Tools,
  Warning,
  TrendCharts
} from '@element-plus/icons-vue'
import * as echarts from 'echarts'

// 响应式数据
const activeTab = ref('equipment')

// 统计数据
const stats = reactive({
  running: 45,
  maintenance: 8,
  fault: 3,
  efficiency: 92
})

// 设备管理相关
const equipmentList = ref([])
const equipmentFilters = reactive({
  code: '',
  name: '',
  type: '',
  status: ''
})
const equipmentPagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 维护计划相关
const maintenanceList = ref([])
const maintenanceFilters = reactive({
  equipmentCode: '',
  type: '',
  status: ''
})
const maintenancePagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 故障管理相关
const faultList = ref([])
const faultFilters = reactive({
  equipmentCode: '',
  level: '',
  status: ''
})
const faultPagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 对话框相关
const equipmentDialogVisible = ref(false)
const equipmentDialogTitle = ref('')
const equipmentForm = reactive({
  id: null,
  code: '',
  name: '',
  type: '',
  model: '',
  manufacturer: '',
  location: '',
  purchaseDate: '',
  purchasePrice: 0,
  description: ''
})
const equipmentFormRef = ref()

const maintenanceDialogVisible = ref(false)
const maintenanceDialogTitle = ref('')
const maintenanceForm = reactive({
  id: null,
  equipmentId: '',
  type: '',
  plannedDate: '',
  responsible: '',
  content: ''
})
const maintenanceFormRef = ref()

const faultDialogVisible = ref(false)
const faultDialogTitle = ref('')
const faultForm = reactive({
  id: null,
  equipmentId: '',
  faultType: '',
  level: '',
  description: '',
  reporter: ''
})
const faultFormRef = ref()

// 设备选项
const equipmentOptions = ref([])

// 图表引用
const statusChartRef = ref()
const efficiencyChartRef = ref()

// 表单验证规则
const equipmentRules = {
  code: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
  type: [{ required: true, message: '请选择设备类型', trigger: 'change' }],
  model: [{ required: true, message: '请输入型号规格', trigger: 'blur' }],
  manufacturer: [{ required: true, message: '请输入制造商', trigger: 'blur' }],
  location: [{ required: true, message: '请输入安装位置', trigger: 'blur' }]
}

const maintenanceRules = {
  equipmentId: [{ required: true, message: '请选择设备', trigger: 'change' }],
  type: [{ required: true, message: '请选择维护类型', trigger: 'change' }],
  plannedDate: [{ required: true, message: '请选择计划日期', trigger: 'change' }],
  responsible: [{ required: true, message: '请输入负责人', trigger: 'blur' }],
  content: [{ required: true, message: '请输入维护内容', trigger: 'blur' }]
}

const faultRules = {
  equipmentId: [{ required: true, message: '请选择设备', trigger: 'change' }],
  faultType: [{ required: true, message: '请输入故障类型', trigger: 'blur' }],
  level: [{ required: true, message: '请选择故障等级', trigger: 'change' }],
  description: [{ required: true, message: '请输入故障描述', trigger: 'blur' }],
  reporter: [{ required: true, message: '请输入报告人', trigger: 'blur' }]
}

// 初始化模拟数据
const initMockData = () => {
  // 设备列表模拟数据
  equipmentList.value = [
    {
      id: 1,
      code: 'EQ001',
      name: '数控车床A',
      type: 'production',
      model: 'CNC-2000',
      manufacturer: '沈阳机床',
      location: '车间A-01',
      status: 'running',
      efficiency: 95,
      purchaseDate: '2022-01-15',
      purchasePrice: 500000,
      description: '高精度数控车床'
    },
    {
      id: 2,
      code: 'EQ002',
      name: '铣床B',
      type: 'production',
      model: 'Mill-3000',
      manufacturer: '大连机床',
      location: '车间A-02',
      status: 'maintenance',
      efficiency: 88,
      purchaseDate: '2022-03-20',
      purchasePrice: 350000,
      description: '通用铣床设备'
    },
    {
      id: 3,
      code: 'EQ003',
      name: '检测仪C',
      type: 'testing',
      model: 'Test-500',
      manufacturer: '精密仪器',
      location: '质检室',
      status: 'running',
      efficiency: 92,
      purchaseDate: '2022-05-10',
      purchasePrice: 120000,
      description: '精密检测设备'
    }
  ]
  equipmentPagination.total = equipmentList.value.length

  // 维护计划模拟数据
  maintenanceList.value = [
    {
      id: 1,
      planCode: 'MP001',
      equipmentCode: 'EQ001',
      equipmentName: '数控车床A',
      type: 'preventive',
      plannedDate: '2024-02-15',
      actualDate: '',
      responsible: '张师傅',
      status: 'planned',
      cost: 5000,
      content: '定期保养维护'
    },
    {
      id: 2,
      planCode: 'MP002',
      equipmentCode: 'EQ002',
      equipmentName: '铣床B',
      type: 'corrective',
      plannedDate: '2024-02-10',
      actualDate: '2024-02-10',
      responsible: '李师傅',
      status: 'ongoing',
      cost: 8000,
      content: '主轴维修'
    }
  ]
  maintenancePagination.total = maintenanceList.value.length

  // 故障管理模拟数据
  faultList.value = [
    {
      id: 1,
      faultCode: 'FT001',
      equipmentCode: 'EQ002',
      equipmentName: '铣床B',
      faultType: '机械故障',
      level: 'serious',
      reportTime: '2024-02-08 14:30:00',
      reporter: '操作员A',
      status: 'processing',
      handler: '李师傅',
      description: '主轴异响'
    },
    {
      id: 2,
      faultCode: 'FT002',
      equipmentCode: 'EQ001',
      equipmentName: '数控车床A',
      faultType: '电气故障',
      level: 'minor',
      reportTime: '2024-02-07 09:15:00',
      reporter: '操作员B',
      status: 'resolved',
      handler: '张师傅',
      description: '控制面板指示灯故障'
    }
  ]
  faultPagination.total = faultList.value.length

  // 设备选项
  equipmentOptions.value = equipmentList.value.map(item => ({
    id: item.id,
    code: item.code,
    name: item.name
  }))
}

// 初始化图表
const initCharts = () => {
  nextTick(() => {
    // 设备状态分布图
    const statusChart = echarts.init(statusChartRef.value)
    const statusOption = {
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: '设备状态',
          type: 'pie',
          radius: '50%',
          data: [
            { value: 45, name: '运行中' },
            { value: 8, name: '维护中' },
            { value: 3, name: '故障' },
            { value: 2, name: '停机' }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    statusChart.setOption(statusOption)

    // 设备效率趋势图
    const efficiencyChart = echarts.init(efficiencyChartRef.value)
    const efficiencyOption = {
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['设备效率']
      },
      xAxis: {
        type: 'category',
        data: ['1月', '2月', '3月', '4月', '5月', '6月']
      },
      yAxis: {
        type: 'value',
        min: 80,
        max: 100
      },
      series: [
        {
          name: '设备效率',
          type: 'line',
          data: [88, 90, 92, 89, 94, 92],
          smooth: true
        }
      ]
    }
    efficiencyChart.setOption(efficiencyOption)
  })
}

// 标签页切换
const handleTabChange = (tabName: string) => {
  console.log('切换到标签页:', tabName)
}

// 设备管理相关方法
const searchEquipment = () => {
  console.log('搜索设备:', equipmentFilters)
}

const resetEquipmentFilters = () => {
  Object.assign(equipmentFilters, {
    code: '',
    name: '',
    type: '',
    status: ''
  })
}

const handleAdd = () => {
  equipmentDialogTitle.value = '新增设备'
  Object.assign(equipmentForm, {
    id: null,
    code: '',
    name: '',
    type: '',
    model: '',
    manufacturer: '',
    location: '',
    purchaseDate: '',
    purchasePrice: 0,
    description: ''
  })
  equipmentDialogVisible.value = true
}

const handleEdit = (row: any) => {
  equipmentDialogTitle.value = '编辑设备'
  Object.assign(equipmentForm, row)
  equipmentDialogVisible.value = true
}

const handleView = (row: any) => {
  ElMessage.info(`查看设备: ${row.name}`)
}

const handleMaintenance = (row: any) => {
  ElMessage.info(`设备维护: ${row.name}`)
}

const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确定要删除设备 "${row.name}" 吗？`, '确认删除', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
  })
}

const handleEquipmentSubmit = () => {
  equipmentFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      ElMessage.success(equipmentForm.id ? '更新成功' : '创建成功')
      equipmentDialogVisible.value = false
    }
  })
}

// 维护计划相关方法
const searchMaintenance = () => {
  console.log('搜索维护计划:', maintenanceFilters)
}

const resetMaintenanceFilters = () => {
  Object.assign(maintenanceFilters, {
    equipmentCode: '',
    type: '',
    status: ''
  })
}

const handleAddMaintenance = () => {
  maintenanceDialogTitle.value = '新增维护计划'
  Object.assign(maintenanceForm, {
    id: null,
    equipmentId: '',
    type: '',
    plannedDate: '',
    responsible: '',
    content: ''
  })
  maintenanceDialogVisible.value = true
}

const handleEditMaintenance = (row: any) => {
  maintenanceDialogTitle.value = '编辑维护计划'
  Object.assign(maintenanceForm, row)
  maintenanceDialogVisible.value = true
}

const handleViewMaintenance = (row: any) => {
  ElMessage.info(`查看维护计划: ${row.planCode}`)
}

const handleExecuteMaintenance = (row: any) => {
  ElMessage.info(`执行维护计划: ${row.planCode}`)
}

const handleMaintenanceSubmit = () => {
  maintenanceFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      ElMessage.success(maintenanceForm.id ? '更新成功' : '创建成功')
      maintenanceDialogVisible.value = false
    }
  })
}

// 故障管理相关方法
const searchFault = () => {
  console.log('搜索故障:', faultFilters)
}

const resetFaultFilters = () => {
  Object.assign(faultFilters, {
    equipmentCode: '',
    level: '',
    status: ''
  })
}

const handleAddFault = () => {
  faultDialogTitle.value = '故障报告'
  Object.assign(faultForm, {
    id: null,
    equipmentId: '',
    faultType: '',
    level: '',
    description: '',
    reporter: ''
  })
  faultDialogVisible.value = true
}

const handleViewFault = (row: any) => {
  ElMessage.info(`查看故障: ${row.faultCode}`)
}

const handleProcessFault = (row: any) => {
  ElMessage.info(`处理故障: ${row.faultCode}`)
}

const handleResolveFault = (row: any) => {
  ElMessage.info(`解决故障: ${row.faultCode}`)
}

const handleFaultSubmit = () => {
  faultFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      ElMessage.success(faultForm.id ? '更新成功' : '创建成功')
      faultDialogVisible.value = false
    }
  })
}

// 通用方法
const handleImport = () => {
  ElMessage.info('导入设备数据')
}

const handleExport = () => {
  ElMessage.info('导出设备数据')
}

// 分页相关方法
const handleEquipmentSizeChange = (size: number) => {
  equipmentPagination.size = size
}

const handleEquipmentCurrentChange = (current: number) => {
  equipmentPagination.current = current
}

const handleMaintenanceSizeChange = (size: number) => {
  maintenancePagination.size = size
}

const handleMaintenanceCurrentChange = (current: number) => {
  maintenancePagination.current = current
}

const handleFaultSizeChange = (size: number) => {
  faultPagination.size = size
}

const handleFaultCurrentChange = (current: number) => {
  faultPagination.current = current
}

// 状态标签相关方法
const getEquipmentTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    production: 'primary',
    testing: 'success',
    auxiliary: 'info'
  }
  return tagMap[type] || ''
}

const getEquipmentTypeText = (type: string) => {
  const textMap: Record<string, string> = {
    production: '生产设备',
    testing: '检测设备',
    auxiliary: '辅助设备'
  }
  return textMap[type] || type
}

const getStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    running: 'success',
    maintenance: 'warning',
    fault: 'danger',
    stopped: 'info'
  }
  return tagMap[status] || ''
}

const getStatusText = (status: string) => {
  const textMap: Record<string, string> = {
    running: '运行中',
    maintenance: '维护中',
    fault: '故障',
    stopped: '停机'
  }
  return textMap[status] || status
}

const getMaintenanceTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    preventive: 'primary',
    corrective: 'warning',
    improvement: 'success'
  }
  return tagMap[type] || ''
}

const getMaintenanceTypeText = (type: string) => {
  const textMap: Record<string, string> = {
    preventive: '预防性维护',
    corrective: '纠正性维护',
    improvement: '改进性维护'
  }
  return textMap[type] || type
}

const getMaintenanceStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    planned: 'info',
    ongoing: 'warning',
    completed: 'success',
    cancelled: 'danger'
  }
  return tagMap[status] || ''
}

const getMaintenanceStatusText = (status: string) => {
  const textMap: Record<string, string> = {
    planned: '计划中',
    ongoing: '进行中',
    completed: '已完成',
    cancelled: '已取消'
  }
  return textMap[status] || status
}

const getFaultLevelTag = (level: string) => {
  const tagMap: Record<string, string> = {
    minor: 'info',
    normal: 'primary',
    serious: 'warning',
    urgent: 'danger'
  }
  return tagMap[level] || ''
}

const getFaultLevelText = (level: string) => {
  const textMap: Record<string, string> = {
    minor: '轻微',
    normal: '一般',
    serious: '严重',
    urgent: '紧急'
  }
  return textMap[level] || level
}

const getFaultStatusTag = (status: string) => {
  const tagMap: Record<string, string> = {
    pending: 'warning',
    processing: 'primary',
    resolved: 'success',
    closed: 'info'
  }
  return tagMap[status] || ''
}

const getFaultStatusText = (status: string) => {
  const textMap: Record<string, string> = {
    pending: '待处理',
    processing: '处理中',
    resolved: '已解决',
    closed: '已关闭'
  }
  return textMap[status] || status
}

// 生命周期
onMounted(() => {
  initMockData()
  initCharts()
})
</script>

<style scoped>
.equipment-management {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-left h2 {
  margin: 0 0 8px 0;
  color: #303133;
  font-size: 24px;
  font-weight: 600;
}

.header-left p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.stats-cards {
  margin-bottom: 20px;
}

.stat-card {
  border: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: #fff;
}

.stat-icon.running {
  background: linear-gradient(135deg, #67c23a, #85ce61);
}

.stat-icon.maintenance {
  background: linear-gradient(135deg, #e6a23c, #f0c78a);
}

.stat-icon.fault {
  background: linear-gradient(135deg, #f56c6c, #f78989);
}

.stat-icon.efficiency {
  background: linear-gradient(135deg, #409eff, #79bbff);
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
}

.charts-section {
  margin-bottom: 20px;
}

.main-content {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.filter-section {
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  margin-bottom: 20px;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .equipment-management {
    padding: 10px;
  }
  
  .page-header {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }
  
  .header-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  
  .stats-cards .el-col {
    margin-bottom: 16px;
  }
  
  .filter-section .el-form {
    flex-direction: column;
  }
  
  .filter-section .el-form-item {
    margin-right: 0;
    margin-bottom: 16px;
  }
}
</style>